const PlayerController = require("../Player/PlayerController");
var LevelManager = require("LevelManager");
var SFXManager = require("SFXManager");

var PlayerHealthController = cc.Class({
    extends: cc.Component,

    properties: {
        currentHealth: 0,
        maxHealth: 0,

        healthBar: cc.ProgressBar,
    },

    statics: {
        instance: null,
    },
    ctor () {
        PlayerHealthController.instance = this;
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
		this.maxHealth = require("PlayerStatController").instance.health[0].value;

        this.currentHealth = this.maxHealth;
    },

    update (dt) {
    },

    // #region HEALTH

    TakeDamage (damage) {
        this.currentHealth -= damage;
        // this.schedule(function () {
        //     this.healthBar.progress -= 1 / this.maxHealth;
        // },0.3, damage, 0);
        // cc.tween(this.healthBar)
        // .to(1.5, {progress: this.currentHealth / this.maxHealth}, {easing: 'sineIn'})
        // .start();
		this.healthBar.progress = this.currentHealth / this.maxHealth;

        if (this.currentHealth <= 0) {
            this.currentHealth = 0;
			PlayerController.instance.playerNode.active = false;
            SFXManager.instance.playSFX(10);

			LevelManager.instance.endLevel();
        }
    },

    // #endregion

    
});
module.exports = PlayerHealthController;
